package com.cltech.frame.base;

import com.cltech.frame.result.ResponseBean;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Created by Administrator on 2018/8/18.
 */
public interface BaseService<T, ID> {
    ResponseBean<List<T>> findAll();

    ResponseBean<List<T>> findAll(Sort sort);

    ResponseBean<List<T>> findAllById(Iterable<ID> ids);

    <S extends T> ResponseBean<S> save(S entity);

    <S extends T> ResponseBean<List<S>> saveAll(Iterable<S> iterables);

    <S extends T> ResponseBean<S> saveAndFlush(S entity);

    void deleteById(ID id);

    void delete(T entity);

    void deleteAll(Iterable<? extends T> iterables);

    void deleteAll();

    void deleteInBatch(Iterable<T> iterables);

    void deleteAllInBatch();

    boolean existsById(ID id);

    ResponseBean<Optional<T>> findById(ID id);

    ResponseBean<T> getOne(ID id);

    <S extends T> ResponseBean<List<S>> findAll(Example<S> example);

    <S extends T> ResponseBean<List<S>> findAll(Example<S> example, Sort sort);

    ResponseBean<Optional<T>> findOne(@Nullable Specification<T> specification);

    ResponseBean<List<T>> findAll(@Nullable Specification<T> specification);

    Page<T> findAll(@Nullable Specification<T> specification, Pageable pageable);

    ResponseBean<List<T>> findAll(@Nullable Specification<T> specification, Sort sort);

    <S extends T> ResponseBean<Optional<S>> findOne(Example<S> var1);

    <S extends T> ResponseBean<Page<S>> findAll(Example<S> var1, Pageable var2);

    <S extends T> long count(Example<S> var1);

    <S extends T> boolean exists(Example<S> var1);

    long count(@Nullable Specification<T> specification);

    /**
     * 原生查询列表.
     * @param clazz 返回POJO
     * @param sql sql
     * @param values 参数
     * @param <D> 返回POJO
     * @return 返回POJO
     */
    <D> ResponseBean<List<D>> listBySQL(Class<D> clazz, String sql, List<Object> values);
    /**
     * 原生查询列表.
     * @param sql sql
     * @param values 参数
     * @return 返回map
     */
    ResponseBean<List<Map<String, Object>>> listBySQL(String sql, List<Object> values);
    /**
     * 原生sql查询数量.
     * @param sql sql
     * @param values values
     * @return 数量
     */
    Long countBySQL(String sql, List<Object> values);

}
